<?php

        // Sample form to upload and insert an image into an ORACLE BLOB
        // column using PHP5's OCI8 API.
        //
        // Before running this script, execute these statements in SQL*Plus:
        //   drop table btab;
        //   create table btab (blobid number, blobdata blob);
        //
        // This example uploads an JPG file and inserts it into a BLOB
        // column.  The image is retrieved back from the column and displayed.
        // Make sure there is no whitespace before "<?php" else the wrong HTTP
        // header will be sent and the image won't display properly.
        //
        // Based on a sample originally found in
        //     http://www.php.net/manual/en/function.ocinewdescriptor.php

        $myblobid = 1;  // should really be a unique id e.g. a sequence number

        define("ORA_CON_UN", "cscd494");             // username
        define("ORA_CON_PW", "R3tral435");             // password
        define("ORA_CON_DB", "//146.187.134.17:1542/oracle10g"); // connection string

        if (!isset($_FILES['lob_upload'])) {
        ?>

        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"
           enctype="multipart/form-data">
        Image filename: <input type="file" name="lob_upload">
        <input type="submit" value="Upload">
        </form>

        <?php
        }
        else {
          
          $blobName = $_FILES['lob_upload']['name'];

          $conn = oci_connect(ORA_CON_UN, ORA_CON_PW, ORA_CON_DB);

          // Delete any existing BLOB so the query at the bottom
          // displays the new data

          $query = 'DELETE FROM BTAB WHERE BLOBID = :MYBLOBID';
          $stmt = oci_parse ($conn, $query);
          oci_bind_by_name($stmt, ':MYBLOBID', $myblobid);
          $e = oci_execute($stmt, OCI_COMMIT_ON_SUCCESS);
          if (!$e) {
            die;
          }
          oci_free_statement($stmt);

          // Insert the BLOB from PHP's tempory upload area

          $lob = oci_new_descriptor($conn, OCI_D_LOB);
          $stmt = oci_parse($conn, 'INSERT INTO BTAB (BLOBID, FILE_NAME, BLOBDATA) '
                 .'VALUES(:MYBLOBID, :BLOBNAME, EMPTY_BLOB()) RETURNING BLOBDATA INTO :BLOBDATA');
          oci_bind_by_name($stmt, ':MYBLOBID', $myblobid);
          oci_bind_by_name($stmt, ':BLOBNAME', $blobName);
          oci_bind_by_name($stmt, ':BLOBDATA', $lob, -1, OCI_B_BLOB);
          oci_execute($stmt, OCI_DEFAULT);

          // The function $lob->savefile(...) reads from the uploaded file.
          // If the data was already in a PHP variable $myv, the
          // $lob->save($myv) function could be used instead.
          if ($lob->savefile($_FILES['lob_upload']['tmp_name'])) {
            oci_commit($conn);
          }
          else 
          {
            echo "Couldn't upload Blob\n";
          }
          $lob->free();
          oci_free_statement($stmt);

          // Now query the uploaded BLOB and display it

          $query = 'SELECT BLOBDATA FROM BTAB WHERE BLOBID = :MYBLOBID';

          $stmt = oci_parse ($conn, $query);
          oci_bind_by_name($stmt, ':MYBLOBID', $myblobid);
          oci_execute($stmt, OCI_DEFAULT);
          $arr = oci_fetch_assoc($stmt);
          $result = $arr['BLOBDATA']->load();

          // If any text (or whitespace!) is printed before this header is sent,
          // the text won't be displayed and the image won't display properly.
          // Comment out this line to see the text and debug such a problem.
          header("Content-type: application/java-archive");
          echo $result;

          oci_free_statement($stmt);

          oci_close($conn); // log off
        }
        ?>
